package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.service.CmsDataDictionaryService;


@Controller
@RequestMapping("/hfmanager/data_dictionary/list.do")
public class CmsDataDictionaryController {
private final Log log = LogFactory.getLog(CmsDocumentController.class);
	
	private final String baseUrl = "data_dictionary";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";

	@Autowired
	private CmsDataDictionaryService cmsDataDictionaryService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		int x = cmsDataDictionaryService.delete(id);
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&x="+x), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsDataDictionaryBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsDataDictionaryService.update(obj);
		}else{
			x = cmsDataDictionaryService.insert(obj);
		}
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&x="+x), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsDataDictionaryBean obj) {
		int id = StringUtil.requestInt(request, "id");
		Map<String, Object> m = new HashMap<String, Object>();
		CmsDataDictionaryBean info = cmsDataDictionaryService.get(id);
		if (info == null)
			info = obj;
		m.put("info", info);
		List<TreeNodeBean> parentlist = cmsDataDictionaryService.getTreePage();
		m.put("parent", parentlist);
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsDataDictionaryBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		List<TreeNodeBean> list = cmsDataDictionaryService.getTreePage();
		model.put("info", list);
		return new ModelAndView(url_list, model);
	}

}
